/********************************************************************/
/*                    Avoider Game                                  */
/*                                                                  */
/*  The object of this game is to move the mouse to avoid the       */
/*  incoming enemies.                                               */
/*                                                                  */
/*  Authors: Chris McBride & Brian Danilczyk                        */
/********************************************************************/

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.util.*;
    import java.text.*;
    import java.io.*;

    public class Avoider extends JFrame {
       
       private static final long serialVersionUID = 1L;
       static int x1, y1, x2, y2, x3, y3, x4, y4, x5, y5;   // Coords of objects
       static JPanel drawSurface;  // This is the panel on which the actual
                            // drawing is done.

       boolean Run = false; //variable to start game
       boolean NewGame = true; //variable to show new game screen
       boolean freeze = false; //variable to freeze screen
       model mod;
       controller control;
       Image image;
       String hs, name, finalScore, lowestHighScore;
       StopWatch s = new StopWatch();
       DecimalFormat secs = new DecimalFormat("###.###");
       
       public Avoider(){
		   super("Avoider");
             // objects placed in starting postions

          x1 = 220;  // Set up initial positions of the squares.
          y1 = 220;
          x2 = -30;
          y2 = -30;
          x3 = 220;
          y3 = 500;
          x4 = 500;
          y4 = 0;
          x5 = 500;
          y5 = 280;
          control = new controller(this); 
          mod = new model(x1, y1, x2, y2, x3, y3, x4, y4, x5, y5, this, control);
         
         image =null;
         try //load background image
         {
            image = javax.imageio.ImageIO.read(new java.net.URL(getClass().getResource("stars.jpg"), "stars.jpg"));
		}catch (Exception e) { /*handled in paintComponent()*/ }

      drawSurface = new JPanel()
      { private static final long serialVersionUID = 2L;
                    // This class defines the drawing
                    // surface for the game.
        public void paintComponent(Graphics g)
        {
         // Draw graphics on panel
         if (image != null)
            g.drawImage(image, 0,0,this.getWidth(),this.getHeight(),this);
         if(Run==false && NewGame)
         {
            g.setColor(Color.red);
            g.setFont(new Font("sansserif", Font.BOLD, 24));
            g.drawString("**AVOIDER**",160,35);

            for(int i=1;i<11;i++)
            {
                hs = mod.getHighscore(i); //get score from hash
                Scanner scan = new Scanner(hs); //scan in high scores
                scan.useDelimiter(";"); //use semicolon for selimiter

                finalScore = scan.next(); //get score then name
                name = scan.next();

                g.setColor(Color.white); //write high scores
                g.setFont(new Font("sansserif", Font.BOLD, 18));
                g.drawString(name,120,65+(i*35));
                g.drawString(secs.format((Long.parseLong(finalScore)/1000.000)),320,65+(i*35));

                if(i==10) // the lowest score on the list
                    lowestHighScore = finalScore;
            }
            g.setColor(Color.white); //create new game screen
            g.setFont(new Font("sansserif", Font.BOLD, 18));
            g.drawString("Click anywhere to start a new game.",70,460);
         }
         else //DRAWS ALL OBJECTS ON SCREEN
         {
            g.setColor(Color.white);
            if(!freeze){
            g.drawString("Time:" + secs.format(s.getElapsedTime()/1000.000),5,12);}
            g.setColor(Color.red);
            g.fill3DRect(x1, y1, 30, 30, true);
            g.setColor(Color.green);
            g.fillOval(x2, y2, 30, 30);
            g.setColor(Color.magenta);
            g.fillOval(x3, y3, 30, 30);
            g.setColor(Color.yellow);
            g.fillOval(x4, y4, 30, 30);
            g.setColor(Color.blue);
            g.fillOval(x5, y5, 30, 30);
            g.setColor(Color.black);
            if(freeze){
			   g.setColor(Color.white);
			   g.setFont(new Font("sansserif", Font.BOLD, 18));
			   g.drawString("Time: " + secs.format(s.getElapsedTime()/1000.000),200,200);	
               g.drawString("Click anywhere to continue",130,250);}
            g.drawRect(0,0,getSize().width-1,getSize().height-1);
         }
       }
     };

         setContentPane(drawSurface);
         drawSurface.addMouseMotionListener(control); //adds mouse listeners
         drawSurface.addMouseListener(control);
         }
 
 public boolean getRun()
 {
	 return Run;
 }
 
  public boolean getFreeze()
 {
	 return freeze;
 }
 
 public int getx1()
 {
	 return x1;
 }
 
  public int gety1()
 {
	 return y1;
 }

  public void set1(int xx, int yy)
 {
	 x1 = xx;
	 y1 = yy;
 }
 
  public void set2(int xx, int yy)
 {
	 x2 = xx;
	 y2 = yy;
 }
 
  public void set3(int xx, int yy)
 {
	 x3 = xx;
	 y3 = yy;
 }
 
  public void set4(int xx, int yy)
 {
	 x4 = xx;
	 y4 = yy;
 }
 
  public void set5(int xx, int yy)
 {
	 x5 = xx;
	 y5 = yy;
 }
 
 public void setFreeze(boolean free)
 {
	 freeze = free;
 }
 
  public void setNewGame(boolean newgame)
 {
	 NewGame = newgame;
 }
 
   public void setRun(boolean rungame)
 {
	 Run = rungame;
 }
 
   public void setStop(boolean stop)
 {
	 if(stop)
		s.start();
	 else
	    s.stop();
 }
 
	public String getTime()
{
	return secs.format(s.getElapsedTime());
}

	public String getTimeMs()
{
	return secs.format(s.getElapsedTime()/1000.000);
}

	public String getLowestScore()
	{
		return lowestHighScore;
	}

} // end 
